home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / DIBLK.PAK / DIBDOC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  227 lines

  1. // dibdoc.cpp : implementation of the CDibDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "diblook.h"
  15. #include <limits.h>
  16.  
  17. #include "dibdoc.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDibDoc
  26.  
  27. IMPLEMENT_DYNCREATE(CDibDoc, CDocument)
  28.  
  29. BEGIN_MESSAGE_MAP(CDibDoc, CDocument)
  30.     //{{AFX_MSG_MAP(CDibDoc)
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CDibDoc construction/destruction
  36.  
  37. CDibDoc::CDibDoc()
  38. {
  39.     m_hDIB = NULL;
  40.     m_palDIB = NULL;
  41.     m_sizeDoc = CSize(1,1);     // dummy value to make CScrollView happy
  42. }
  43.  
  44. CDibDoc::~CDibDoc()
  45. {
  46.     if (m_hDIB != NULL)
  47.     {
  48.         ::GlobalFree((HGLOBAL) m_hDIB);
  49.     }
  50.     if (m_palDIB != NULL)
  51.     {
  52.         delete m_palDIB;
  53.     }
  54. }
  55.  
  56. BOOL CDibDoc::OnNewDocument()
  57. {
  58.     if (!CDocument::OnNewDocument())
  59.         return FALSE;
  60.     return TRUE;
  61. }
  62.  
  63. void CDibDoc::InitDIBData()
  64. {
  65.     if (m_palDIB != NULL)
  66.     {
  67.         delete m_palDIB;
  68.         m_palDIB = NULL;
  69.     }
  70.     if (m_hDIB == NULL)
  71.     {
  72.         return;
  73.     }
  74.     // Set up document size
  75.     LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
  76.     if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
  77.     {
  78.         ::GlobalUnlock((HGLOBAL) m_hDIB);
  79.         ::GlobalFree((HGLOBAL) m_hDIB);
  80.         m_hDIB = NULL;
  81.         CString strMsg;
  82.         strMsg.LoadString(IDS_DIB_TOO_BIG);
  83.         MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  84.         return;
  85.     }
  86.     m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
  87.     ::GlobalUnlock((HGLOBAL) m_hDIB);
  88.     // Create copy of palette
  89.     m_palDIB = new CPalette;
  90.     if (m_palDIB == NULL)
  91.     {
  92.         // we must be really low on memory
  93.         ::GlobalFree((HGLOBAL) m_hDIB);
  94.         m_hDIB = NULL;
  95.         return;
  96.     }
  97.     if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
  98.     {
  99.         // DIB may not have a palette
  100.         delete m_palDIB;
  101.         m_palDIB = NULL;
  102.         return;
  103.     }
  104. }
  105.  
  106.  
  107. BOOL CDibDoc::OnOpenDocument(LPCTSTR lpszPathName)
  108. {
  109.     CFile file;
  110.     CFileException fe;
  111.     if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
  112.     {
  113.         ReportSaveLoadException(lpszPathName, &fe,
  114.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  115.         return FALSE;
  116.     }
  117.  
  118.     DeleteContents();
  119.     BeginWaitCursor();
  120.  
  121.     // replace calls to Serialize with ReadDIBFile function
  122.     TRY
  123.     {
  124.         m_hDIB = ::ReadDIBFile(file);
  125.     }
  126.     CATCH (CFileException, eLoad)
  127.     {
  128.         file.Abort(); // will not throw an exception
  129.         EndWaitCursor();
  130.         ReportSaveLoadException(lpszPathName, eLoad,
  131.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  132.         m_hDIB = NULL;
  133.         return FALSE;
  134.     }
  135.     END_CATCH
  136.  
  137.     InitDIBData();
  138.     EndWaitCursor();
  139.  
  140.     if (m_hDIB == NULL)
  141.     {
  142.         // may not be DIB format
  143.         CString strMsg;
  144.         strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
  145.         MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  146.         return FALSE;
  147.     }
  148.     SetPathName(lpszPathName);
  149.     SetModifiedFlag(FALSE);     // start off with unmodified
  150.     return TRUE;
  151. }
  152.  
  153.  
  154. BOOL CDibDoc::OnSaveDocument(LPCTSTR lpszPathName)
  155. {
  156.     CFile file;
  157.     CFileException fe;
  158.  
  159.     if (!file.Open(lpszPathName, CFile::modeCreate |
  160.       CFile::modeReadWrite | CFile::shareExclusive, &fe))
  161.     {
  162.         ReportSaveLoadException(lpszPathName, &fe,
  163.             TRUE, AFX_IDP_INVALID_FILENAME);
  164.         return FALSE;
  165.     }
  166.  
  167.     // replace calls to Serialize with SaveDIB function
  168.     BOOL bSuccess = FALSE;
  169.     TRY
  170.     {
  171.         BeginWaitCursor();
  172.         bSuccess = ::SaveDIB(m_hDIB, file);
  173.         file.Close();
  174.     }
  175.     CATCH (CException, eSave)
  176.     {
  177.         file.Abort(); // will not throw an exception
  178.         EndWaitCursor();
  179.         ReportSaveLoadException(lpszPathName, eSave,
  180.             TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
  181.         return FALSE;
  182.     }
  183.     END_CATCH
  184.  
  185.     EndWaitCursor();
  186.     SetModifiedFlag(FALSE);     // back to unmodified
  187.  
  188.     if (!bSuccess)
  189.     {
  190.         // may be other-style DIB (load supported but not save)
  191.         //  or other problem in SaveDIB
  192.         CString strMsg;
  193.         strMsg.LoadString(IDS_CANNOT_SAVE_DIB);
  194.         MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  195.     }
  196.  
  197.     return bSuccess;
  198. }
  199.  
  200. void CDibDoc::ReplaceHDIB(HDIB hDIB)
  201. {
  202.     if (m_hDIB != NULL)
  203.     {
  204.         ::GlobalFree((HGLOBAL) m_hDIB);
  205.     }
  206.     m_hDIB = hDIB;
  207. }
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210. // CDibDoc diagnostics
  211.  
  212. #ifdef _DEBUG
  213. void CDibDoc::AssertValid() const
  214. {
  215.     CDocument::AssertValid();
  216. }
  217.  
  218. void CDibDoc::Dump(CDumpContext& dc) const
  219. {
  220.     CDocument::Dump(dc);
  221. }
  222.  
  223. #endif //_DEBUG
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CDibDoc commands
  227.